<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_Login extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	function __construct()
	{
		parent::__construct();
		
		$this->load->model('m_impact', '', true);
		$this->load->model('m_user', '', true);			
	}
	
	/*
	**Default
	*/
	function index()
	{
		if($this->session->userdata('name'))
			redirect(site_url().'/c_main', 'location', 302);
					
		$this->load->view('v_login');
	}
	/*
	**Login
	*/
	function verifylogin(){
		$password = $this->input->post('password');
		$email 	  = $this->input->post('email');
		
		$result = $this->m_user->login($email,'');
		
		// check for result 
		if (sizeof($result) > 0) {
			$salt = $result[0]['salt'];
			$encrypted_password = $result[0]['encrypted_password'];
			$hash = $this->m_user->checkhashSSHA($salt, $password);
			// check for password equality
			if ($encrypted_password == $hash) {
				// user authentication details are correct
				//return $result;
				//add all data to session
				 $newdata = array(
							'gro_id'			=> $result[0]['gro_id'],
							'group_level'		=> $result[0]['level'],
							'uid' 				=> $result[0]['uid'],
							'name' 				=> $result[0]['name'],
							'email'     		=> $result[0]['email'],
							'logged_in' 		=> TRUE
					   );
				$this->session->set_userdata($newdata);
				
				redirect(site_url().'/c_main', 'location', 302);
			}else{
				//message
				$this->session->set_userdata('message', $this->connect->message("Invalid password!", "error"));
				$this->index();
			}
		} else {
			//message
			$this->session->set_userdata('message', $this->connect->message("Invalid email and password!", "error"));
			$this->index();
		}
 	}
	
	/*
	check user email
	*/
	function check_user()
	{
		$usr=$this->input->post('email_address');
		$result=$this->m_user->check_user_exist($usr);
		if($result)
		{
			echo "false";
			
		}
		else{
			
			echo "true";
		}
	}
	
	/*
	Destroy session
	*/
	function logout()
	{
		$newdata = array(
							'gro_id'			=> '',
							'group_level'		=> '',
							'uid' 				=> '',
							'cus_id' 			=> '',
							'name' 				=> '',
							'email'     		=> '',
							'logged_in' 		=> FALSE
					   );
		$this->session->unset_userdata($newdata);
		$this->session->sess_destroy();
		redirect(site_url().'/c_login', 'location', 302);
	}
}
/* End of file welcome.php */
/* Location: ./application/controllers/login.php */





